//
//  SopTextViewController.m
//  DigiOps
//
//  Created by app on 2018/8/10.
//  Copyright © 2018年 俞渊华. All rights reserved.
//

#import "SopTextViewController.h"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
#import "WBPopMenuView.h"
#define TEXT_H 120.0f//!<文本输入框高度
#define SEL_H 44.0f//!<选项高度

@interface SopTextViewController ()<UITextViewDelegate>
{
    CGFloat _prompt_H;//!<prompt提示文字高度

}
@property(nonatomic,strong)UITextView* textview;
@end

@implementation SopTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(clickEvent)];
//    [myButton setTintColor:[UIColor whiteColor]];
//    self.navigationItem.rightBarButtonItem = myButton;
    
    self.navigationItem.hidesBackButton = YES;
    UIImage *image = [UIImage imageNamed:@"Back"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftItemsSupplementBackButton = YES;
    
    _prompt_H=0;
    self.textview = [[UITextView alloc] init];
    
    self.textview.delegate = self;
    
    self.textview.text = @"请在此处输入";
    if (_saveText&&![_saveText isEqualToString:@""]) {
        self.textview.text=_saveText;
    }
    self.textview.textColor = [UIColor colorWithWhite:0.6 alpha:0.6];
    self.textview.font = [UIFont systemFontOfSize:15];
    self.textview.layer.cornerRadius = 5;
    self.textview.layer.borderWidth = 0.5;
    self.textview.layer.borderColor = [UIColor colorWithWhite:0.3 alpha:0.3].CGColor;
    self.textview.clipsToBounds = YES;
    [self.view addSubview:self.textview];
    
    [self.textview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(SEL_H/4+_prompt_H);
        make.width.mas_equalTo(self.view.bounds.size.width-SEL_H/2);
        make.height.mas_equalTo(@120);
        make.centerX.mas_equalTo(self.view);
    }];
}

-(void)goBack
{
    [self clickEvent];
}
-(void)clickEvent{
    [self sendTxt];
//    NSMutableArray *obj = [NSMutableArray array];
//
//    for (NSInteger i = 0; i < [self titles].count; i++) {
//
//        WBPopMenuModel * info = [WBPopMenuModel new];
//        //        info.image = [self images][i];
//        info.title = [self titles][i];
//        [obj addObject:info];
//    }
//
//    [[WBPopMenuSingleton shareManager]showPopMenuSelecteWithFrame:150
//                                                             item:obj
//                                                           action:^(NSInteger index) {
//                                                               NSLog(@"index:%ld",(long)index);
//                                                               [self sendTxt];
//                                                           }];
}
- (NSArray *) titles {
    return @[@"保存"];
}

- (NSArray *) images {
    return @[@"right_menu_QR@3x",
             @"right_menu_addFri@3x",
             @"right_menu_multichat@3x",
             @"right_menu_sendFile@3x",
             @"right_menu_facetoface@3x",
             @"right_menu_payMoney@3x"];
}
-(void)sendTxt
{
    if ([_textview.text isEqualToString:@"请在此处输入"]) {
        _textview.text = @"";
    }
    if (self.block) {
        self.block(_textview.text);
    }
    [self.rt_navigationController popViewControllerAnimated:YES complete:nil];
}
/**实现place holder效果,开始编辑时,清除hint
 */
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    //    NSLog(@"开始编辑");
    
    if ([textView.text isEqualToString:@"请在此处输入"]) {
        textView.text = @"";
    }
    textView.textColor = [UIColor blackColor];
    
//    self.keyboardBlock(textView);
}

/**实现place holder效果,结束编辑时,如未输入,恢复显示hint
 */
-(void)textViewDidEndEditing:(UITextView *)textView
{
    //    NSLog(@"结束编辑");
    
    if (textView.text.length == 0) {
        textView.text = @"请在此处输入";
        textView.textColor = [UIColor colorWithWhite:0.6 alpha:0.6];
    }
}

/**当点击回车时,结束输入,键盘收起
 */
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]){
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
